define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/coupons/index' + location.search,
                    add_url: 'user/coupons/add',
                    edit_url: 'user/coupons/edit',
                    del_url: 'user/coupons/del',
                    multi_url: 'user/coupons/multi',
                    import_url: 'user/coupons/import',
                    table: 'coupons',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'full', title: __('Full')},
                        {field: 'reduces', title: __('Reduces')},
                        {field: 'num', title: __('Num')},
                        {field: 'sort', title: __('Sort')},
                        {field: 'send', title: __('Send'),table: table,
                            buttons: [{
                                name: '',text: "发放优惠券",title: "发放优惠券",classname: 'btn btn-xs btn-primary btn-dialog',url:'user/index/index'
                            }],
                            formatter: function (value,row,index){
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            events:Table.api.events.operate,operate:false
                        },
                        // {field: 'type', title: __('Type')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
